package xin.yangshuai.springsecurity01.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import xin.yangshuai.springsecurity01.dao.SpringSecurityUserMapper;
import xin.yangshuai.springsecurity01.model.SpringSecurityUser;
import xin.yangshuai.springsecurity01.model.SpringSecurityUserExample;

import java.util.ArrayList;
import java.util.List;

@Component
public class DatabaseUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {

    @Autowired
    private SpringSecurityUserMapper springSecurityUserMapper;

    @Override
    public UserDetails updatePassword(UserDetails userDetails, String s) {
        return null;
    }

    @Override
    public void createUser(UserDetails userDetails) {

        SpringSecurityUser springSecurityUser = new SpringSecurityUser();
        springSecurityUser.setUsername(userDetails.getUsername());
        springSecurityUser.setPassword(userDetails.getPassword());

        springSecurityUserMapper.insert(springSecurityUser);
    }

    @Override
    public void updateUser(UserDetails userDetails) {

    }

    @Override
    public void deleteUser(String s) {

    }

    @Override
    public void changePassword(String s, String s1) {

    }

    @Override
    public boolean userExists(String s) {
        return false;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        List<GrantedAuthority> authorityList = new ArrayList<>();
//        authorityList.add(new GrantedAuthority() {
//            @Override
//            public String getAuthority() {
//                return "USER_LIST";
//            }
//        });
        authorityList.add(new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return "USER_ADD";
            }
        });

        SpringSecurityUserExample example = new SpringSecurityUserExample();
        SpringSecurityUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<SpringSecurityUser> userList = springSecurityUserMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(userList)) {
            throw new UsernameNotFoundException(username);
        } else {
            SpringSecurityUser springSecurityUser = userList.get(0);
//            User user = new User(springSecurityUser.getUsername(), springSecurityUser.getPassword(), authorityList);

            // 添加role属性，需要使用builder

            UserDetails user = User.builder()
                    .username(springSecurityUser.getUsername())
                    .password(springSecurityUser.getPassword())
                    // roles 与 authorities 不能同时使用，会被覆盖
                    // .roles("ADMIN")
                    .authorities("USER_ADD","USER_UPDATE")
                    .build();
            return user;
        }
    }
}
